vimfandomcom-20200223-history
Specify a column with bar
You may well be aware that '|' is the goto column motion, and "75|" will place your cursor on column 75 of the current line. That in itself is pretty handy at times, but some true power arises when used in conjuction with visual mode and replace. v75|r- will replace from the cursor to the end of line with '-'. Explanation: v start visual mode 75 for the count | goto column r replace - the character for the replace A handy and quick way to make a noticeable section of your code (or whatever). A handy way to use this (formatted to just drop into DrChip's CStubs): "// -Feral--------------------------------------------------------------- "// elseif wrd "//" exe "norm! a -AuthorId\$lv75|r-$a\\$a " "// -Feral:146/02@08:31-------------------------------------------------- "// elseif wrd "///" exe "norm! s -AuthorId:\=strftime('%j/%y@%H:%M')\\$lv75|r-$a\\$a " "/* -Feral:146/02@08:31-------------------------------------------------- " * " * -------------------------------------------------------------------- */ elseif wrd "/*" exe "norm! a -AuthorId:\=strftime('%j/%y@%H:%M')\\$lv75|r-$a\\\2lv72|r-$a */\k$a " Comments How can I move the "bar" in foo bar to column 30? ---- That is also quite easy to do as it turns out. "move everything after the cursor to column count" equates to: d$30|p Explanation: d$ to del to end of line (into unnamed register) 30| to place cursor on column 30 p to paste unnamed register This tip made no sense to me until I ":set virtualedit=all", so if it's not working for you, try that. ---- For some reason | wont go beyond the newline character and is completely meaningless on an empty line. So I just do "75i-". ---- That is because you need: :set virtualedit=all The problem with just 75i- is that it will inset 75 chars(or strings as case may be) regardless of what column you are in. So, to trim the line at column 75 you would need to add 75|D (goto column 75 and delete to end of line). There is an advantage to doing things that way however, one it works regardless of how you have virtualedit set (I have a hard time fathoming it being anything but all personally ;) ) but you can also specify a string to repeate, thus "=-=-=-=-=-=-=-=-=-=-=-" or some such is possible (75i=-75|D) The same idea holds true for positioning the rest of a line at a specific column: //place cursor on start of word you want at column 30i 30|dw (goto column 30 and delete word (i.e. the extra spaces) ) ---- Is there a way to use this to align variable declarations? Say I have three spaces then the variable name then some unknown number of spaces and the type information. If I want to align all the type information at column 20, how would I do that? I essentially need to eliminate the leading whitespace before pasting I think... ---- Sort of messy but given: varname typeinfo varname typeinfo varname typeinfo 0weldw20i 20|dw on each line will result in: varname typeinfo varname typeinfo varname typeinfo Explanation: 0 = start of line, column 0 wel = place cursor one char past first word (var name) dw = delete spaces to next word (type definition) 20i = instert 20 spaces. 20|dw = goto column 20 and delete the spaces from here to next word (type definition) ---- See for a simple script to save you from "ciw" to align text. Which is relevant to the alignment of variables question/answer above. ----